package install.ren.exercise.test.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;

/**
 * @author jonny
 * Created on 2021/8/29
 */
@Aspect
public class ProfileAspect {
    // -javaagent:/Users/jonny/repo/org/aspectj/aspectjweaver/1.9.2/aspectjweaver-1.9.2.jar
    // -javaagent:/Users/jonny/repo/org/springframework/spring-instrument/5.1.4.RELEASE/spring-instrument-5.1.4.RELEASE.jar

    @Around("profileMethod()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch(getClass().getName());
        try {
            sw.start(pjp.getSignature().getName());
            return pjp.proceed();
        } finally {
            sw.stop();
            System.err.println(pjp.getSignature() + ":" + sw.getTotalTimeMillis());
        }
    }
    @Around("profileMethodApollo()")
    public Object profileApollo(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch(getClass().getName());
        try {
            sw.start(pjp.getSignature().getName());
            return pjp.proceed();
        } finally {
            sw.stop();
            System.err.println(pjp.getSignature() + ":" + sw.getTotalTimeMillis());
        }
    }
    @Pointcut("execution(public * install.ren.exercise.test.aop.*.*(..))")
    public void profileMethod() {
    }
    @Pointcut("execution(public * com.ctrip.framework.apollo.*.*(..))")
    public void profileMethodApollo() {
    }
}
